package day20171117;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.*;

/**
 * Created by 14394 on 2017/11/17.
 */
public class TestJsoupForPic {
    public static void main(String[] args) throws IOException {
        String imageSrc="http://p3-q.mafengwo.net/s7/M00/9D/13/wKgB6lSosqaAWed_AAYcHZfMZD0122.png?imageMogr2%2Fthumbnail%2F%211020x540r%2Fgravity%2FCenter%2Fcrop%2F%211020x540%2Fquality%2F100";
        Connection.Response response= Jsoup.connect(imageSrc).ignoreContentType(true).execute();
        byte[] img=response.bodyAsBytes();
        System.out.println(img.length);
        saveImage(img,"F:\\dev\\DowloadTest","拉萨.png");
    }

    private static void saveImage(byte[] img, String filePath, String fileName) {
        BufferedOutputStream bos=null;
        FileOutputStream fos=null;
        File file=null;
        File dir=new File(filePath);
        try {
            //判断文件目录是否存在
            if (!dir.exists()){
                dir.mkdir();
            }
            file=new File(filePath+"\\"+fileName);
            fos=new FileOutputStream(file);
            bos=new BufferedOutputStream(fos);
            bos.write(img);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
